/**
 * @author : retainv
 * @date : 2024/3/15 15:24
 */
public class Leetcode443压缩字符串 {

    public static void main(String[] args) {
        char[] chars = {'a','a','a','b','b','a','a'};
        int compress = compress(chars);
    }
    public static int compress(char[] chars) {
        if (chars == null || chars.length == 0) return 0;

        int anchor = 0, write = 0;
        for (int read = 1; read <= chars.length; read++) {
            if (read == chars.length || chars[read] != chars[anchor]) {
                chars[write++] = chars[anchor];
                if (read - anchor > 1) {
                    for (char c : Integer.toString(read - anchor).toCharArray()) {
                        chars[write++] = c;
                    }
                }
                anchor = read;
            }
        }
        return write;
    }
}
